package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.aliyun.iot20180120.models.QueryDevicePropertyStatusRequest;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceDataMapper deviceDataMapper;

    @Override
    public PageResponse getPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(),deviceDataPageReqDto.getPageSize());

        Long startTime = deviceDataPageReqDto.getStartTime();
        Long endTime = deviceDataPageReqDto.getEndTime();

        deviceDataPageReqDto.setStartDate(ObjectUtil.isEmpty(startTime) ? null : LocalDateTimeUtil.of(startTime));
        deviceDataPageReqDto.setEndDate(ObjectUtil.isEmpty(endTime) ? null : LocalDateTimeUtil.of(endTime));

        Page<DeviceDataVo>page=deviceDataMapper.list(deviceDataPageReqDto.getDeviceName(),deviceDataPageReqDto.getEndDate(),
                deviceDataPageReqDto.getEndDate(),deviceDataPageReqDto.getFunctionId());

        return PageResponse.of(page,DeviceDataVo.class);
    }




    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        LocalDateTime localDateTime = LocalDateTimeUtil.of(startTime);
        LocalDateTime localDateTime2 = LocalDateTimeUtil.of(endTime);

//        List<DeviceDataGraphVo>dataGraphVoList=deviceDataMapper.queryDeviceDataListByDay(iotId,functionId,LocalDateTimeUtil.of(startTime),LocalDateTimeUtil.of(endTime));
//
//        List<DeviceDataGraphVo> deviceDataGraphVoList = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));
//
//        Map<String, Double> map = deviceDataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
//
//        dataGraphVoList.forEach(d->{
//            double val = map.get(d.getDateTime()) == null ? 0.0 : map.get(d.getDateTime());
//            d.setDataValue(val);
//        });
//
//        return dataGraphVoList;


        List<DeviceDataGraphVo>dataGraphVoList=deviceDataMapper.queryDeviceDataListByDay(iotId,functionId,LocalDateTimeUtil.of(startTime),LocalDateTimeUtil.of(endTime));

        List<DeviceDataGraphVo> deviceDataGraphVoList = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));

        Map<String, Double> collect = dataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        deviceDataGraphVoList.forEach(d->{
            double v = collect.get(d.getDateTime()) == null ? 0.0 : collect.get(d.getDateTime());
            d.setDataValue(v);
        });
        return deviceDataGraphVoList;
    }



    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
//        List<DeviceDataGraphVo>dataGraphVoList=deviceDataMapper.queryDeviceDataListByWeek(iotId,functionId,LocalDateTimeUtil.of(startTime),LocalDateTimeUtil.of(endTime));
//
//        List<DeviceDataGraphVo> deviceDataGraphVoList = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));
//        Map<String, Double> map = deviceDataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
//
//        dataGraphVoList.forEach(d->{
//            double val = map.get(d.getDateTime()) == null ? 0.0 : map.get(d.getDateTime());
//            d.setDataValue(val);
//        });
//        return dataGraphVoList;


        List<DeviceDataGraphVo>dataGraphVoList=deviceDataMapper.queryDeviceDataListByWeek(iotId,functionId,LocalDateTimeUtil.of(startTime),LocalDateTimeUtil.of(endTime));

        List<DeviceDataGraphVo> deviceDataGraphVoList = DeviceDataGraphVo.weekInstance(LocalDateTimeUtil.of(startTime));

        Map<String, Double> map = dataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));

        deviceDataGraphVoList.forEach(d->{
            double v = map.get(d.getDateTime()) == null ? 0.0 : map.get(d.getDateTime());
            d.setDataValue(v);
        });
        return deviceDataGraphVoList;
    }
}
